package person.bing.fbcloud.leetcode;

/**
 * @author: mengfanbing
 * @date: 2020/10/13 10:45
 */
public class Q33 {
    public static void main(String[] args) {
        int[] nums = {4,5,6,7,0,1,2};
        int target = 3;
        int search = search(nums, target);
        System.out.println(search);
    }
    public static int search(int[] nums, int target) {
        if (nums == null){
            return -1;
        }
        int length = nums.length;
        if (length == 0){
            return -1;
        }
        if (length == 1){
            if (target == nums[0]){
                return 0;
            }
            return -1;
        }
        int start = 0;
        int end = length - 1;
        while (start < end){
            int med = (start + end) / 2;
            int startNum = nums[start];
            int endNum = nums[end];
            int medNum = nums[med];
            int medLastNum = nums[med + 1];

            if (medNum == target){
                return med;
            }
            if (startNum == target){
                return start;
            }
            if (endNum == target){
                return end;
            }
            if (medLastNum == target){
                return med+1;
            }
            if (medNum>startNum){
                if (target>startNum && target<medNum){
                    end = med;
                    continue;
                }else {
                    start = med+1;
                    continue;
                }
            }else {
                if (target>medLastNum && target<endNum){
                    start = med+1;
                    continue;
                }else {
                    end = med;
                    continue;
                }
            }
        }
        return -1;
    }
}
